package com.sitechasia.webx.components.xtable.cell;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.taglib.tiles.GetAttributeTag;
import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.cell.Cell;
import org.extremecomponents.table.cell.HeaderCell;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.core.TableModelUtils;
import org.extremecomponents.table.view.html.BuilderUtils;
import org.extremecomponents.table.view.html.ColumnBuilder;
import org.extremecomponents.table.view.html.TableActions;
import org.extremecomponents.util.HtmlBuilder;

/**
*
* <p>版权所有：中企动力
* <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
* <p>功能描述：生成列表HeaderCell
*
* @author kuanglingxuan
* Created on 2007-8-2
*/
public class PmsHeaderCell implements Cell
{

    public PmsHeaderCell()
    {
    }

    public String getExportDisplay(TableModel model, Column column)
    {
        return column.getTitle();
    }

    public String getHtmlDisplay(TableModel model, Column column)
    {
        HtmlBuilder html = new HtmlBuilder();
        String headerClass = null;
        String sortImage = null;
        String sortOrder = null;
        headerClass = column.getHeaderClass();
        if(TableModelUtils.isSorted(model, column.getAlias())){
            sortOrder = model.getLimit().getSort().getSortOrder();
            if(sortOrder.equals("default"))
                sortOrder = "asc";
            else if(sortOrder.equals("asc")){
                sortImage = "images/ico_table_up.gif";//BuilderUtils.getImage(model, "sortAsc");
                sortOrder = "desc";
            } else
            if(sortOrder.equals("desc"))
            {
//                headerClass = model.getPreferences().getPreference("table.headerSortClass");
                sortImage = "images/ico_table_down.gif";//BuilderUtils.getImage(model, "sortDesc");
                sortOrder = "default";
            }
        } else{
            sortOrder = "asc";
        }
        buildHeaderHtml(html, model, column, headerClass, sortImage, sortOrder);
        return html.toString();
    }


    protected void buildHeaderHtml(HtmlBuilder html, TableModel model, Column column, String headerClass, String sortImage, String sortOrder)
    {
        html.td(2);
        //styleClass
        String tdClass = column.getStyleClass();
        if(StringUtils.isBlank(tdClass))
         tdClass = model.getPreferences().getPreference("table.headtable.tdClass");
        html.styleClass(tdClass);
        //onMouseOver
        String onmouseover = column.getAttributeAsString("headerOnmouseover");
        if(StringUtils.isBlank(onmouseover))
    	   onmouseover = model.getPreferences().getPreference("table.headtable.onmouseover");
        //onMouseOut属性
        String onmouseout = column.getAttributeAsString("headerOnmouseout");
        if(StringUtils.isBlank(onmouseout))
    	   onmouseout  = model.getPreferences().getPreference("table.headtable.onmouseout");

        String tdWrith = column.getWidth();
        html.width(tdWrith).styleClass(tdClass).onmouseover(onmouseover).onmouseout(onmouseout);

        if(column.isSortable() && model.getCollectionOfBeans().size() > 0){
            html.onclick((new TableActions(model)).getSortAction(column, sortOrder));
            boolean showTooltips = model.getTableHandler().getTable().isShowTooltips();
            if(showTooltips){
                String headercellTooltip = model.getMessages().getMessage("column.headercell.tooltip.sort");
                html.title(headercellTooltip + " " + column.getTitle());
            }
        }
        html.close();
        if(StringUtils.isNotEmpty(headerClass))
        	html.append("<span class=\""+ column.getHeaderClass()+"\">");
        html.append(column.getTitle());

        if(column.isSortable() && StringUtils.isNotEmpty(sortImage))
        {
            html.nbsp();
            html.img();
            html.src(sortImage);
//            html.style("border:0");
//            html.alt("Arrow");
            html.xclose();
        }
        if(StringUtils.isNotEmpty(headerClass))
        	html.append("</span>");
        html.tdEnd();
    }
}
